<?php


namespace common\components;


use common\helpers\TransformApi;
use common\models\Activity;
use common\models\ActivityEnter;
use common\models\GoodsSpec;
use common\models\Order;
use common\models\OrderGoods;
use common\models\OrderLog;
use yii\base\BaseObject;
use yii\queue\JobInterface;

class OrderBehavior  extends BaseObject implements JobInterface
{
    const Act_CANCEL = 'ActivityCancel';
    const ORDER_CANCEL = 'OrderCancel';
    const ORDER_RECEIV = 'OrderReceiv';
    public $method;
    public $params;

    public function execute($queue)
    {
        if (!method_exists($this, $this->method)) {
            return false;
        }
        $method = $this->method;
        return $this->$method();
    }

    /**
     * 活动关闭
     * @return bool
     */
    public function ActivityCancel()
    {
        $params = $this->params;
        if (empty($params['id'])){
            return false;
        }
        $model = ActivityEnter::find()->where(['id'=>$params['id'],'status'=>0])->one();
        if (!$model){
            return false;
        }
        $activity = Activity::find()->where(['id'=>$model->act_id])->one();
        $activity->surplus += $model->num;
        $model->status = 2;
        $model->save();
        $activity->save();
        return true;
    }
    /**
     * 订单关闭
     * @return bool
     */
    public function OrderCancel()
    {
        $params = $this->params;
        if (empty($params['id'])){
            return false;
        }
        $order = Order::find()->where(['id'=>$params['id']])->select(['order_status','id'])->one();
        if (!$order){
            return false;
        }
        if ((int)$order->order_status!==0){
            return false;
        }
        $ordergoods = OrderGoods::find()->select(['spec_id','good_num'])->where(['order_id'=>$order->id])->groupBy(['spec_id'])->asArray()->all();
        $order->order_status = 4;
        if ($order->save()){
            $update_id = [];
            $update = [];
            foreach ($ordergoods as $ordergood){
                $valu = $ordergood['good_num'];
                $update[] = [
                    'surplus'=>"`surplus`+$valu"
                ];
                $update_id[] = $ordergood['spec_id'];
            }
            if ($update){
                $sql = TransformApi::batchUpdate(GoodsSpec::getTableSchema()->name,$update,$update_id);
                \Yii::$app->db->createCommand($sql)->execute();
            }
            OrderLog::addLog('',$params['id'],'系统订单关闭','',3);
        }else{
            return false;
        }
        return true;
    }

    /**
     * 订单收货
     * @return bool
     */
    public function OrderReceiv()
    {
        $params = $this->params;
        if (empty($params['id'])){
            return false;
        }
        $order = Order::find()->where(['id'=>$params['id']])->select(['order_status','id'])->one();
        if (!$order){
            return false;
        }
        if ((int)$order->order_status!==2){
            return false;
        }
        $order->order_status = 3;
        if ($order->save()){
            OrderLog::addLog('',$params['id'],'订单收货','',3);
        }else{
            return false;
        }
        return true;
    }
}